package com.br.framework.util.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.br.framework.util.lang.StringUtil;

public class FileUtil {
	/**
	 * filePath 에 있는 파일을 읽어서 String 으로 line 단위로 List 를 반환 한다. <br>
	 * @param filePath
	 * @return List
	 * @throws IOException
	 */
	public static List<String> getFileToList(String filePath) throws IOException {
		BufferedReader in = null;
		List<String> list = null;

		try {
			in = new BufferedReader(new FileReader(filePath));
			list = new ArrayList<String>();
			String s = null;

			while ((s = in.readLine()) != null) {
				list.add(s);
			}
		}
		finally {
			close(in);
		}

		return list;
	}




	/**
	 * filePath 에 해당 하는 File 을 읽어서 String 으로 <br>
	 * 반환 한다.<br>
	 * @param filePath
	 * @param str
	 * @return String
	 * @throws IOException
	 */
	public static String getFileToStringBuilder(String filePath)
			throws IOException {
		BufferedReader in = null;
		StringBuilder builder = null;

		try {
			in = new BufferedReader(new FileReader(filePath));
			builder = new StringBuilder();
			String s = null;

			while ((s = in.readLine()) != null) {
				builder.append(s);
			}
		}
		finally {
			close(in);
		}

		return builder.toString();
	}

	/**
	 * 파일이 존재 하는지 확인 한다. <br>
	 * 존재 하면 true 아니면 false 를 반환 한다. <br>
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public static boolean exists(String path) throws Exception {
		File f = new File(path);
		if (f.exists())
			return true;
		else
			return false;
	}

	/**
	 * Reader
	 * @param reader
	 * @throws IOException
	 */
	public static void close(Reader reader) throws IOException {
		if (reader != null) {
			try {
				reader.close();
			}
			finally{
				reader = null;
			}
		}
	}

	/**
    *
    * 파일종류 가지고 오기
    * 파일 확장자의 종류가 이미지 인지 동영상인지, 문서 인지 웹문서, 압축파일, 실행파일 인지 가지고 오기
	 * @param path
	 * @return img, mov, doc, web, etc, zip, exe
	 * @date 2009. 5. 25
	 */
   public static String getFileKind(String path){
      String rst="";
      String fileExe=StringUtil.substringAfter(path,".");
      String img[]={"ani","bmp","dib","clp","cmx","cut","lbm","pcx","cur","dcx","dhp","drw","dxf","emf","gem","img","gif","grf","ico","jpg","jpeg","ms","msp","pic","png","psd"};
      String mov[]={"asf","au","avi","cda","cmx","cmv","flc","fli","flv","m3u","mid","rmi","mov","qt","mp3","mp2","mp1"," mpg","mpeg","ra","ram","rm","ra","ram","rmm","rmi","mid","voc","wav"};
      String doc[]={"ann","asc","asv","bak","bk!","cha","cpe","csv","dic","dif","dir","diz","doc","dot","eml","gal","gul","hwp","hwt","log","pdf","ppt","txt","xla","xlm","xlb","xld","xlw","xll","xlv","xlc","xls"};
      String web[]={"asp","html","htm","jsp","php"};
      String exe[]={"app","bat","cmd","exe"};
      String zip[]={"ace","ais","arc","arj","cab","gz","ice","pak","rar","tar","taz","zip"};
      if(StringUtil.matchArray(img,fileExe)){
          rst="img";
      }else if(StringUtil.matchArray(mov,fileExe)){
          rst="mov";
      }else if(StringUtil.matchArray(doc,fileExe)){
          rst="doc";
      }else if(StringUtil.matchArray(web,fileExe)){
          rst="web";
      }else if(StringUtil.matchArray(exe,fileExe)){
          rst="exe";
      }else if(StringUtil.matchArray(zip,fileExe)){
          rst="zip";
      }
      return rst;
  }

  /**
   * MINE 정보 가지고 오기
	 * @param path
	 * @return
	 */
  public static String getMINE(String path){
      String rst="";
      String fileExe=StringUtil.substringAfter(path,".");
      String msaccess[]={"mdb"};
      String msword[] = {"doc"};
      String pdf[] = {"pdf"};
      String postscript[] = {"ai", "ps", "eps"};
      String rtf[] = {"rtf","rtf"};
      String excel[] = {"xls"};
      String powerpoint[] = {"ppt"};
      String dvi[] = {"dvi"};
      String javascript[] = {"js"};
      String ms[] = {"ms"};
      String source[] = {"src"};
      String zip[] = {"zip"};
      String aiff[] = {"aiff"};
      String wav[] = {"wav"};
      String gif[] = {"gif"};
      String jpeg[] = {"jpeg", "jpg","jpe"};
      String tiff[] = {"tiff","tif"};
      String gzip[] = {"gzip"};
      String css[] = {"css"};
      String html[] = {"html","htm"};
      String plain[] = {"txt "};
      String xml[] = {"xml"};
      String xsl[] = {"xsl"};
      String mpeg[] = {"mpeg", "mpg", "mpe"};
      String quicktime[] = {"qt", "mov"};
      String msvideo[] = {"avi"};
      String movie[] = {"movie"};
      if(StringUtil.matchArray(msaccess,fileExe)){
         rst="application/msaccess";
     }else if(StringUtil.matchArray(msword,fileExe)){
         rst="application/msword";
     }else if(StringUtil.matchArray(pdf,fileExe)){
         rst="application/pdf";
     }else if(StringUtil.matchArray(postscript,fileExe)){
         rst="application/postscript";
     }else if(StringUtil.matchArray(rtf,fileExe)){
         rst="application/rtf";
     }else if(StringUtil.matchArray(excel,fileExe)){
         rst="application/vnd.ms-excel";
     }else if(StringUtil.matchArray(powerpoint,fileExe)){
         rst="application/vnd.ms-powerpoint";
     }else if(StringUtil.matchArray(dvi,fileExe)){
         rst="application/x-dvi";
     }else if(StringUtil.matchArray(javascript,fileExe)){
         rst="application/x-javascript";
     }else if(StringUtil.matchArray(ms,fileExe)){
         rst="application/x-troff-ms";
     }else if(StringUtil.matchArray(source,fileExe)){
         rst="application/x-wais-source";
     }else if(StringUtil.matchArray(aiff,fileExe)){
         rst="audio/x-aiff";
     }else if(StringUtil.matchArray(wav,fileExe)){
         rst="audio/x-wav";
     }else if(StringUtil.matchArray(gif,fileExe)){
         rst="image/gif";
     }else if(StringUtil.matchArray(jpeg,fileExe)){
         rst="image/jpeg";
     }else if(StringUtil.matchArray(tiff,fileExe)){
         rst="image/tiff";
     }else if(StringUtil.matchArray(gzip,fileExe)){
         rst="multipart/x-zip";
     }else if(StringUtil.matchArray(css,fileExe)){
         rst="text/css";
     }else if(StringUtil.matchArray(html,fileExe)){
         rst="text/html";
     }else if(StringUtil.matchArray(plain,fileExe)){
         rst="text/plain";
     }else if(StringUtil.matchArray(xml,fileExe)){
         rst="text/xml";
     }else if(StringUtil.matchArray(xsl,fileExe)){
         rst="zip";
     }else if(StringUtil.matchArray(zip,fileExe)){
         rst="text/xsl";
     }else if(StringUtil.matchArray(mpeg,fileExe)){
         rst="video/mpeg";
     }else if(StringUtil.matchArray(quicktime,fileExe)){
         rst="video/quicktime";
     }else if(StringUtil.matchArray(msvideo,fileExe)){
         rst="video/x-msvideo";
     }else if(StringUtil.matchArray(movie,fileExe)){
         rst="video/x-sgi-movie";
     }else{
        rst="application/octet-stream";
     }
     return rst;
	}
}
